package com.lyxs916.requestbody;

import com.lyxs916.utils.RequestBodyUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;

/**
 * 说明:retrofit 请求参数封装
 * 作者： 杨阳; 创建于：  2017-06-15  16:23
 */
public class HttpRequestParam {
    Map<String, RequestBody> partMap = new HashMap<>();
    Map<String, String> mapString = new HashMap<>();

    /**
     * 设置 action
     *
     * @param valuse action
     */
    public void addAction(String valuse) {
        addParam("action", valuse);
    }


    public void addParam(String key, String valuse) {
        if (!isEmpty(key) && !isEmpty(valuse)) {
            mapString.put(key, valuse);
        }
    }

    public void addParam(String key, int valuse) {
        addParam(key, String.valueOf(valuse));
    }

    public void addParam(String key, double valuse) {
        addParam(key, String.valueOf(valuse));
    }

    public void addParam(String key, File file) {
        if (file.exists())
            partMap.put(RequestBodyUtils.getFileKey(key, file), RequestBodyUtils.getFileRequestBody(file));

//        FileParam param = new FileParam(key, file);
//        if (param.exists())
//            partMap.put(param.getMapKey(), param.getFileRequestBody());


    }

    public void addParamFile(String key, String filePath) {
        if (!isEmpty(filePath)) {
            File file = new File(filePath);
            if (file.exists()) {
                addParam(RequestBodyUtils.getFileKey(key, file), new File(filePath));
            }
        }
    }

    public void addParam(String key, List<File> files) {
        for (File file : files) {
            if (file.exists())
                partMap.put(RequestBodyUtils.getFileKey(key, file), RequestBodyUtils.getFileRequestBody(file));
        }
    }


    public void addParamFile(String key, List<String> filePaths) {
        for (String path : filePaths) {
            File file = new File(path);
            if (file.exists())
                partMap.put(RequestBodyUtils.getFileKey(key, file), RequestBodyUtils.getFileRequestBody(file));
        }
    }

    public Map<String, RequestBody> getPartMap() {
        for (Map.Entry<String, String> entry : mapString.entrySet()) {
            partMap.put(entry.getKey(), RequestBodyUtils.getStringRequestBody(entry.getValue()));
        }
        return partMap;
    }

    public Map<String, String> getStringMap() {
        return mapString;
    }


    /**
     * 是否正常的字符串
     *
     * @return
     */
    public boolean isEmpty(String str) {
        return (str == null || str.length() == 0);
    }

}
